using System.Collections;
using System.Collections.Generic;
using System.Data;
using OneDayModel.Interfaces;

namespace OneDayModel.DBRepository
{
    public class DbRepository : IRepository
    {
        private readonly IConnectionManager connectionManager;
        private readonly IDbFetcher dbFetcher;

        public DbRepository(IConnectionManager connectionManager, IDbFetcher dbFetcher)
        {
            this.connectionManager = connectionManager;
            this.dbFetcher = dbFetcher;
        }

        public IList<IRole> GetRoles()
        {
            IDbConnection connection = connectionManager.AcquireConnection();
            try
            {
                IDataReader rolesReader = dbFetcher.GetRolesReader(connection);
                return new RolesListOnDataReader(rolesReader);
            }
            finally
            {
                connectionManager.ReleaseConnection(connection);
            }
        }

        public IUser NewUser()
        {
            throw new System.NotImplementedException();
        }

        public bool IsAliasUnique(string alias)
        {
            throw new System.NotImplementedException();
        }

        public IUser GetUserByEmail(string email)
        {
            throw new System.NotImplementedException();
        }

        public void CreateUser(string alias, string email, string password, IEnumerable<IRole> assignedRoles,
                               IEnumerable<KeyValuePair<string, string>> attributes, AccountStatus userAccountStatus)
        {
            throw new System.NotImplementedException();
        }

        public void RegisterConfirmationSent(string alias, string confirmationId)
        {
            throw new System.NotImplementedException();
        }

        public IUser GetUserPendingForConfirmation(string confirmationId)
        {
            throw new System.NotImplementedException();
        }

        public void UpdateUser(string alias, List<KeyValuePair<string, string>> changes)
        {
            throw new System.NotImplementedException();
        }

        public void RegisterConfirmationDone(string confirmationId)
        {
            throw new System.NotImplementedException();
        }

        public IUser GetUserByAlias(string alias)
        {
            throw new System.NotImplementedException();
        }

        public StatisticsStruct CollectStatistics()
        {
            throw new System.NotImplementedException();
        }

        public IDictionary<string, IRoleAttribute> AllAttributesDefinitions
        {
            get { throw new System.NotImplementedException(); }
        }

        public IAlbum RegisterAlbum(IUser user, string albumName)
        {
            throw new System.NotImplementedException();
        }

        public IAlbum GetAlbumForUser(string alias, string albumName)
        {
            throw new System.NotImplementedException();
        }

        public string GetBasePathForAlbums(string userAlias)
        {
            throw new System.NotImplementedException();
        }

        public void RegisterPictureInAlbum(string userAlias, string albumName, string fileName)
        {
            throw new System.NotImplementedException();
        }
    }

    internal class RolesListOnDataReader : IList<IRole>
    {
        private readonly List<IRole> roles = new List<IRole>();

        public RolesListOnDataReader(IDataReader reader)
        {
            InitiateList(reader);
        }

        private void InitiateList(IDataReader dataReader)
        {
            while(dataReader.Read())
            {
                roles.Add(new DbRole(dataReader));
            }
            dataReader.Close();
        }

        public int IndexOf(IRole item)
        {
            throw new System.NotImplementedException();
        }

        public void Insert(int index, IRole item)
        {
            throw new System.NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            throw new System.NotImplementedException();
        }

        public IRole this[int index]
        {
            get { return roles[index]; }
            set { throw new System.NotImplementedException(); }
        }

        public void Add(IRole item)
        {
            throw new System.NotImplementedException();
        }

        public void Clear()
        {
            throw new System.NotImplementedException();
        }

        public bool Contains(IRole item)
        {
            throw new System.NotImplementedException();
        }

        public void CopyTo(IRole[] array, int arrayIndex)
        {
            throw new System.NotImplementedException();
        }

        public bool Remove(IRole item)
        {
            throw new System.NotImplementedException();
        }

        public int Count
        {
            get { return roles.Count; }
        }

        public bool IsReadOnly
        {
            get { return true; }
        }

        IEnumerator<IRole> IEnumerable<IRole>.GetEnumerator()
        {
            throw new System.NotImplementedException();
        }

        public IEnumerator GetEnumerator()
        {
            return ((IEnumerable<IRole>) this).GetEnumerator();
        }
    }
}